package student.aistar.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.Model;
import student.aistar.model.entity.*;
import student.aistar.service.ICartService;
import student.aistar.service.IIndexService;
import student.aistar.service.IProductService;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import org.springframework.web.bind.annotation.RequestParam;
import student.aistar.model.entity.Category;
import student.aistar.model.entity.Navclass;
import student.aistar.model.entity.Navpro;
import student.aistar.model.entity.Product;
import student.aistar.service.IIndexService;
import student.aistar.service.impl.ProductService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    IIndexService iIndexService;

    @Autowired
    ICartService cartService;

    @Autowired
    IProductService productService;

    @GetMapping("/headerCart")
    public String headerCart(Model model, HttpSession session){
        //将查询到的购物车数据和商品数据返回到页面
        User user = (User) session.getAttribute("user");
        if(user == null)
            return "mi/asyn_refresh/header_cart";
        List<Cart> carts = cartService.userCart(user.getUserId());
        List<Product> products = new ArrayList<>();
        Double totalPrice = 0.0;
        Integer totalNum = 0;
        for (int i = 0; i < carts.size(); i++) {
            Product product = productService.findById(carts.get(i).getProductId());
            totalPrice += product.getPrice() * carts.get(i).getNum();
            totalNum += carts.get(i).getNum();
            products.add(product);
        }
        //购物车集合
        model.addAttribute("carts", carts);
        //总价
        model.addAttribute("totalPrice", totalPrice);
        //个数总和
        model.addAttribute("totalNum", totalNum);
        //产品集合
        model.addAttribute("products", products);
        return "mi/asyn_refresh/header_cart";
    }

    /**
     * 向首页展示数据
     * @param
     * @param userId
     * @return
     */
    @GetMapping("/index")
    public String index(HttpServletRequest request, String userId){
        List<Product> productList = iIndexService.indexProductData(1);
        List<Navclass> navClassList = iIndexService.navClassList();

        List<Category> categoryList = productService.getAllCategory();

        request.setAttribute("categoryList",categoryList);
        request.setAttribute("navClassList", navClassList);
        request.setAttribute("productList", productList);
        return "mi/index";
    }
    @PostMapping ("/indexshow")
    public String show(HttpServletRequest request, String userId, String productName, Integer categoryId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "19") Integer pageSize){
        PageInfo<Product> productPageInfo = productService.getProduct(productName, categoryId, pageNum, pageSize);
        request.setAttribute("productPageInfo",productPageInfo);
        return "mi/product/index_show";
    }

    /**
     * 顶部导航栏的下拉框数据
     * @param model
     * @param navId
     * @return
     */
    @PostMapping("/navProduct")
    public String navProduct(Model model, String navId){
        List<Navpro> navProductList = iIndexService.navProList(navId.charAt(0) - '0');
        model.addAttribute("navProductList", navProductList);
        return "mi/asyn_refresh/header_product";
    }



}
